/**
 * Created with JetBrains WebStorm.CACA
 * User: Fred_974CACA
 * Date: 04/06/13CACA
 * Time: 11:18CACA
 * To change this template use File | Settings | File Templates.CACA
 */
var connector = require('./connector');
var connection = connector.connection();
// We need our model
var Weapon = require("./Weapon");

var findDefaultWeapon = function(callback){
    var weapon = new Weapon(); // Our model that we can instanciateCACA
    connection.query('SELECT * from weapon', function(err, rows) { // we do the query
        if (err) throw err; // if there is an error we throw it
        if (rows.length === 1){ // there is one result
            weapon.init(rows[0]); // we init the model with the row
        }else{
            weapon = null;
        }
        callback(weapon); // Callback basicaly it's the end of the controleur
    });
};

var findAll = function(id,callback){

    connection.query('select * from weapon', function(err, rows, fields){

        if (err) throw err; // if there is an error we throw it



        var weapon = new Weapon();
        weapon.init(rows[0]);

        callback(weapon); // Callback basicaly it's the end of the controleur
    });

};

var findAllWeapons = function(callback){

    connection.query('select * from weapon', function(err, rows, fields){

        if (err) throw err; // if there is an error we throw it






        callback(rows); // Callback basicaly it's the end of the controleur

    });

};
var findById = function(id,callback){

    connection.query('select * from weapon where id= ?', [id], function(err, rows, fields){

        if (err) throw err; // if there is an error we throw it


        // if rows length == 0 return null;
        var weapon = new Weapon();
        weapon.init(rows[0]);
        callback(weapon); // Callback basicaly it's the end of the controleur
    });

};



var persistWeaponSettings = function(weapon, callback){

    var post  = {name: weapon.name, requirement: weapon.requirement, damage: weapon.damage, cadence: weapon.cadence,
        price: weapon.price, precision: weapon.precision}



        if(weapon.id == null){

            connection.query('INSERT INTO weapon SET ?', post, function(err, rows, fields){

                if (err) throw err; // if there is an error we throw it
                callback(weapon); // Callback basicaly it's the end of the controleur
            });
        }else{
            connection.query('update weapon SET ? where id = ?',[post,weapon.id], function(err, rows, fields){



                if (err) throw err; // if there is an error we throw it
                callback(weapon); // Callback basicaly it's the end of the controleur
            });

        }


};

var deleteWeapon = function(weapon, callback){



    if(weapon.id == null){

        connection.query('delete from weapon where id ='+weapon.id, function(err, rows, fields){

            if (err) throw err; // if there is an error we throw it
            callback(weapon); // Callback basicaly it's the end of the controleur
        });
    }else{
        connection.query('delete from weapon where id ='+weapon.id, function(err, rows, fields){



            if (err) throw err; // if there is an error we throw it
            callback(weapon); // Callback basicaly it's the end of the controleur
        });

    }
}
/*
var createWeapon = function(weapon, callback){


    var post  = {name: weapon.name, requirement: weapon.requirement, damage: weapon.damage, cadence: weapon.cadence,
        price: weapon.price, precision: weapon.precision};

    console.log(weapon.name);

    if(weapon.id == null){

        connection.query('INSERT INTO weapon SET ?', post, function(err, rows, fields){

            if (err) throw err; // if there is an error we throw it
            callback(weapon); // Callback basicaly it's the end of the controleur
        });
    }else{
        connection.query('INSERT INTO weapon SET ?', post, function(err, rows, fields){



            if (err) throw err; // if there is an error we throw it
            callback(weapon); // Callback basicaly it's the end of the controleur
        });

    }
}*/






exports.persistWeaponSettings = persistWeaponSettings;
exports.findDefaultWeapon = findDefaultWeapon;
exports.findById = findById;
exports.findAll = findAll;
exports.findAllWeapons = findAllWeapons;
exports.deleteWeapon = deleteWeapon;
